<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <!-- <el-form-item label="楼栋编号" prop="spuId">
        <el-input
          v-model="queryParams.spuId"
          placeholder="请输入楼栋编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="房产名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入房产名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerId">
        <!-- <el-input
          v-model="queryParams.customerId"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        /> -->
        <el-select v-model="queryParams.customerId" placeholder="请选择客户名称" filterable>
          <el-option
            v-for="item in customerList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="房产名称" align="center" prop="name" />
      <el-table-column align="center" label="客户" prop="customerName" width="300px">
        <template #default="scope">
          <el-link
            :underline="false"
            type="primary"
            @click="openCustomerDetail(scope.row.customerId)"
          >
            {{ scope.row.customerName }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="旧面积m²" align="center" prop="oldArea" />
      <el-table-column label="新面积m²" align="center" prop="area" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.HOUSE_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import * as CustomerApi from '@/api/crm/customer'
import * as HouseSpuApi from '@/api/house/house/spu'
import { DICT_TYPE } from '@/utils/dict'

/** house 楼栋单元 列表 */
defineOptions({ name: 'HouseSku' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { push } = useRouter()

const loading = ref(true) // 列表的加载中
const list = ref<HouseSpuApi.Sku[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  // spuId: undefined,
  customerId: undefined,
  name: undefined,
  oldArea: undefined,
  area: undefined,
  properties: undefined,
  status: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const customerList = ref<CustomerApi.CustomerVO[]>([])
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await HouseSpuApi.getSkuPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 打开客户详情 */
const openCustomerDetail = (id: number) => {
  push({ name: 'CrmCustomerDetail', params: { id } })
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 初始化 **/
onMounted(async() => {
  customerList.value = await CustomerApi.getCustomerSimpleList()
  await getList()
})
</script>
